﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace thinger.VisionDemo
{
    public partial class FrmSoftKey : Form
    {
        public FrmSoftKey()
        {
            InitializeComponent();
        }
        //防止窗体闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        public static string input = string.Empty;
        public static bool confirm = false;
        //这是 0~9 以及 . 按钮绑定的事件
        void btn_Click(object sender, EventArgs e)
        {
            //Button B = (Button)sender;
            //string buttonText = B.Text;
            //int selectionStart = textBox1.SelectionStart; // 保存当前焦点位置
            //textBox1.Text = textBox1.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
            //textBox1.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
            //textBox1.Focus(); // 重新设置焦点到文本框

            Button B = (Button)sender;
            string buttonText = B.Text;
            //判断里面是否有.
            if (buttonText == ".")
            {
                int index = textBox1.Text.IndexOf('.');
                if (index < 0)
                {
                    int selectionStart = textBox1.SelectionStart; // 保存当前焦点位置
                    textBox1.Text = textBox1.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
                    textBox1.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
                    textBox1.Focus(); // 重新设置焦点到文本框
                }
            }else if(buttonText == "-")
            {
                if (textBox1.Text.Length == 0)
                {
                    int selectionStart = textBox1.SelectionStart; // 保存当前焦点位置
                    textBox1.Text = textBox1.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
                    textBox1.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
                    textBox1.Focus(); // 重新设置焦点到文本框
                }
            }
            else
            {
                int selectionStart = textBox1.SelectionStart; // 保存当前焦点位置
                textBox1.Text = textBox1.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字
                textBox1.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
                textBox1.Focus(); // 重新设置焦点到文本框
            }


        }
        //清空按钮
        void btn_Clr_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox1.Focus();
        }
        //删除按钮
        void btn_Bsc_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                return;
            }
            int selectionStart = textBox1.SelectionStart;
            textBox1.Text = textBox1.Text.Remove(selectionStart - 1, 1);
            textBox1.SelectionStart = selectionStart - 1;
            textBox1.Focus();
        }
        //确定按钮
        void btn_Confirm_Click(object sender, EventArgs e)
        {
            input = textBox1.Text;
            confirm = true;
            this.Close();
        }
        //退出按钮
        private void btn_Esc_Click(object sender, EventArgs e)
        {
            confirm = false;
            this.Close();
        }
        //以下代码是为了获得文本框本来的内容
        private void FrmSoftKey_Shown(object sender, EventArgs e)
        {
            //textBox1.Text = input;
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.SelectionLength = 0;
            textBox1.Focus();
        }
        //限制实体键盘只能输入数字和 . ,可以删掉
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 检查是否是数字或小数点
            if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.'&& e.KeyChar != '-' && !char.IsControl(e.KeyChar))
            {
                e.Handled = true; // 取消输入
            }

            // 确保只能输入一个小数点
            if (e.KeyChar == '.' && ((TextBox)sender).Text.Contains('.'))
            {
                e.Handled = true; // 取消输入
            }

            // 确保只能输入一个减号
            if (e.KeyChar == '-' && ((TextBox)sender).Text.Length>0 )
            {
                e.Handled = true; // 取消输入
            }
        }
        //回车时触发事件，和点击确定是一样的效果
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // 在这里执行回车键触发的事件逻辑
                input = textBox1.Text;
                confirm = true;
                this.Close();
            }
        }
        //窗体添加 LocationChanged 事件，防止键盘弹出位置位于窗体之外
        private void FrmSoftKey_LocationChanged(object sender, EventArgs e)
        {
            CheckAndAdjustFormPosition();
        }
        private void CheckAndAdjustFormPosition()
        {
            Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;

            int newLeft = Math.Min(Math.Max(this.Left, workingArea.Left), workingArea.Right - this.Width);
            int newTop = Math.Min(Math.Max(this.Top, workingArea.Top), workingArea.Bottom - this.Height);

            this.Location = new Point(newLeft, newTop);
        }
    }

}
